﻿using System;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Generic;
using SpiderRelation0720;

public class Test
{
    public static void Main()
    {
        string url = "0";
        string result = "0";
        for (int i = 0; i < 8; )
        {
            i = i + 1;
            url = ("http://202.114.74.199/stu/choose_pubLesson_list_beta.jsp?actionType=choose&pageNum=" + i);
            result = GetPageData(url, "");
            searchClasses s = new searchClasses();
            s.AddClasses(result);
            // Console.WriteLine(result);

            //url = ("http://202.114.74.199/stu/choose_pubLesson_list_beta.jsp?actionType=choose&pageNum=" + i);
        }
        //Console.WriteLine(result);
        // string classPP=@"周(.*?):(\d)-(\d*)周(.*?)\n(\d*)-(\d*)节,(\d)区,(.)-(.*?);";
        //    string path = @"C:\教务部网站.txt";
        //    //searchClasses search = new searchClasses();

        //  //创建并写入(将覆盖已有文件)
        //    //正则    周(.*?):(\d)-(\d*)周(.*?)\n(\d*)-(\d*)节,(\d)区,(.)-(.*?);
        //// if (!File.Exists(path))
        // //{

        //    ////Regex jumpp = new Regex(classPP);
        //    //Match jumpPage = jumpp.Match(result);//进行匹配
        //    //if (jumpPage.Success)
        //    //{
        //    //    List<int> classes = new List<int>();  
        //    //}

        //     using (StreamWriter sw = File.CreateText(path))
        //     {
        //         //StreamReader reader = new StreamReader(sw, System.Text.Encoding.UTF8);
        //         sw.WriteLine(result);

        //     }
        Console.ReadKey();
        //}

    }
    public static string GetPageData(string url, string charSet)
    {
        string strWebData = string.Empty;
        if (url != null || url.Trim() != "")
        {
            WebClient myWebClient = new WebClient();
            //创建WebClient实例myWebClient
            // 需要注意的：
            //有的网页可能下不下来，有种种原因比如需要cookie,编码问题等等
            //这是就要具体问题具体分析比如在头部加入cookie
            myWebClient.Headers.Add("Cookie", "JSESSIONID=0000gGm9bt44ANzoYev6MngJR9n:-1");
            // myWebClient.Headers.Add("请求", "GET /stu/newstu_index.jsp HTTP/1.1");
            myWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU");
            //这样可能需要一些重载方法。根据需要写就可以了
            //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
            myWebClient.Credentials = CredentialCache.DefaultCredentials;
            //如果服务器要验证用户名,密码
            //NetworkCredential mycred = new NetworkCredential("2010302580222", "19920501");
            //myWebClient.Credentials = mycred;
            //从资源下载数据并返回字节数组。（加@是因为网址中间有"/"符号）
            byte[] myDataBuffer = myWebClient.DownloadData(url);
            strWebData = Encoding.Default.GetString(myDataBuffer);
            //获取网页字符编码描述信息
            Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            string webCharSet = charSetMatch.Groups[2].Value;
            if (charSet == null || charSet == "")
            {
                //如果未获取到编码，则设置默认编码
                if (webCharSet == null || webCharSet == "")
                {
                    charSet = "UTF-8";
                }
                else
                {
                    charSet = webCharSet;
                }
            }
            if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default)
            {
                strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);
            }
        }
        return strWebData;
    }
}
